const { ppxxxkkk, adminName, maxDeviceNum } = require('../serverOpt');
const userMod = require('./cloudControl/userMod'); // 更新为正确的路径
const crypto = require('crypto');
const menus = require('../common/menu.js')
const dayjs = require('dayjs')

createDefaultAdmin();

async function createDefaultAdmin() {
	try {
		const useDoc = await userMod.findOne({ userName: adminName });
		if (!useDoc) {
			const newAdmin = new userMod({
				userName: adminName,
				userPass: encrypt('123456'),
				maxDeviceNum: maxDeviceNum,
				beiZhu: '超级管理员',
				parent: '111111111111111111111111',
				userLevel: 0,
				checkStatus: true, //是否审核通过
				createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'), //加入时间
				permissionList:JSON.stringify(menus)
			});
			await newAdmin.save();
		}
	} catch (error) {
		console.error('创建超级管理员出错:', error);
	}
}
function encrypt(text) {
	const iv = crypto.randomBytes(16); // 初始化向量
	const cipher = crypto.createCipheriv('aes-256-cbc', ppxxxkkk, iv);
	let encrypted = cipher.update(text, 'utf8', 'hex');
	encrypted += cipher.final('hex');
	return { iv: iv.toString('hex'), encryptedData: encrypted };
}
